<?php

namespace ticky;

class schema {

    public static $tablename;

    public static function set_tablename($tablename) {
        self::$tablename = db()->prefix . $tablename;
    }

    public static function sql_create($tablename) {
        self::set_tablename($tablename);
        self::sql_delete($tablename);
        $sql = "CREATE TABLE `" . self::$tablename . "` (
			  `id` int(10) unsigned NOT NULL auto_increment,
			  `uid` mediumint(8) unsigned NOT NULL DEFAULT '0',
			  `inputtime` int(10) unsigned NOT NULL DEFAULT '0',
			  PRIMARY KEY (`id`)
			) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
        self::sql_exec($sql);
    }

    public static function sql_delete($tablename) {
        self::set_tablename($tablename);
        $sql = "DROP TABLE IF EXISTS `" . self::$tablename . "`";
        self::sql_exec($sql);
    }

    public static function sql_add_field($tablename, $field, $defaultvalue = '', $maxlength = 250) {
        self::set_tablename($tablename);
        $sql = "ALTER TABLE `" . self::$tablename . "` ADD COLUMN `$field` varchar($maxlength) NOT NULL DEFAULT '$defaultvalue'";
        self::sql_exec($sql);
    }

    public static function sql_add_field_mediumtext($tablename, $field) {
        self::set_tablename($tablename);
        $sql = "ALTER TABLE `" . self::$tablename . "` ADD COLUMN `$field` mediumtext NOT NULL";
        self::sql_exec($sql);
    }

    public static function sql_add_field_text($tablename, $field) {
        self::set_tablename($tablename);
        $sql = "ALTER TABLE `" . self::$tablename . "` ADD COLUMN `$field` text NOT NULL";
        self::sql_exec($sql);
    }

    public static function sql_add_field_int($tablename, $field, $defaultvalue = 0) {
        self::set_tablename($tablename);
        $sql = "ALTER TABLE `" . self::$tablename . "` ADD COLUMN `$field` int(10) UNSIGNED NOT NULL DEFAULT $defaultvalue";
        self::sql_exec($sql);
    }

    public static function sql_del_field($tablename, $field) {
        self::set_tablename($tablename);
        $sql = "ALTER TABLE `" . self::$tablename . "` DROP COLUMN `$field`";
        self::sql_exec($sql);
    }

    public static function sql_exec($sql) {
        global $model;
        $model = isset($model) ? $model : db();
        $model->query($sql);
    }

}
